home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / util / proper~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  6.9 KB  |  264 lines

  1. /*
  2.  * @(#)Properties.java    1.21 95/12/15 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.util;
  21.  
  22. import java.io.IOException;
  23. import java.io.PrintStream;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;
  26. import java.util.Hashtable;
  27.  
  28. /**
  29.  * Persistent properties class. This class is basically a hashtable 
  30.  * that can be saved/loaded from a stream. If a property is not found,
  31.  * a property list containing defaults is searched. This allows
  32.  * arbitrary nesting.
  33.  *
  34.  * @author Arthur van Hoff
  35.  * @version     1.21, 12/15/95
  36.  */
  37. public
  38. class Properties extends Hashtable {
  39.     protected Properties defaults;
  40.  
  41.     /**
  42.      * Creates an empty property list.
  43.      */
  44.     public Properties() {
  45.     this(null);
  46.     }
  47.  
  48.     /**
  49.      * Creates an empty property list with specified defaults.
  50.      * @param defaults the defaults
  51.      */
  52.     public Properties(Properties defaults) {
  53.     this.defaults = defaults;
  54.     }
  55.  
  56.     /**
  57.      * Loads properties from an InputStream.
  58.      * @param in the input stream
  59.      * @exception IOException Error when reading from input stream.
  60.      */
  61.     public synchronized void load(InputStream in) throws IOException {
  62.     in = Runtime.getRuntime().getLocalizedInputStream(in);
  63.  
  64.     int ch = in.read();
  65.     while (true) {
  66.         switch (ch) {
  67.           case -1:
  68.         return;
  69.  
  70.           case '#':
  71.           case '!':
  72.         do {
  73.             ch = in.read();
  74.         } while ((ch >= 0) && (ch != '\n') && (ch != '\r'));
  75.         continue;
  76.  
  77.           case '\n':
  78.           case '\r':
  79.           case ' ':
  80.           case '\t':
  81.         ch = in.read();
  82.         continue;
  83.         }
  84.  
  85.         // Read the key
  86.         StringBuffer key = new StringBuffer();
  87.         while ((ch >= 0) && (ch != '=') && (ch != ':') && 
  88.            (ch != ' ') && (ch != '\t') && (ch != '\n') && (ch != '\r')) {
  89.         key.append((char)ch);
  90.         ch = in.read();
  91.         }
  92.         while ((ch == ' ') && (ch == '\t')) {
  93.         ch = in.read();
  94.         }
  95.         if ((ch == '=') || (ch == ':')) {
  96.         ch = in.read();
  97.         }
  98.         while ((ch == ' ') && (ch == '\t')) {
  99.         ch = in.read();
  100.         }
  101.  
  102.         // Read the value
  103.         StringBuffer val = new StringBuffer();
  104.         while ((ch >= 0) && (ch != '\n') && (ch != '\r')) {
  105.         if (ch == '\\') {
  106.             switch (ch = in.read()) {
  107.               case '\n': 
  108.             while (((ch = in.read()) == ' ') || (ch == '\t'));
  109.             continue;
  110.               case 't': ch = '\t'; break;
  111.               case 'n': ch = '\n'; break;
  112.               case 'r': ch = '\r'; break;
  113.               case 'u': {
  114.             while ((ch = in.read()) == 'u');
  115.             int d = 0;
  116.               loop:
  117.             for (int i = 0 ; i < 4 ; i++, ch = in.read()) {
  118.                 switch (ch) {
  119.                   case '0': case '1': case '2': case '3': case '4':
  120.                   case '5': case '6': case '7': case '8': case '9':
  121.                 d = (d << 4) + ch - '0';
  122.                 break;
  123.                   case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  124.                 d = (d << 4) + 10 + ch - 'a';
  125.                 break;
  126.                   case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  127.                 d = (d << 4) + 10 + ch - 'A';
  128.                 break;
  129.                   default:
  130.                 break loop;
  131.                 }    
  132.             }
  133.             ch = d;
  134.               }
  135.             }
  136.         }
  137.         val.append((char)ch);
  138.         ch = in.read();
  139.         }
  140.  
  141.         //System.out.println(key + " = '" + val + "'");
  142.         put(key.toString(), val.toString());
  143.     }
  144.     }
  145.  
  146.     /**
  147.      * Save properties to an OutputStream. Use the header as
  148.      * a comment at the top of the file.
  149.      */
  150.     public synchronized void save(OutputStream out, String header) {
  151.     OutputStream localOut = Runtime.getRuntime().getLocalizedOutputStream(out);
  152.     PrintStream prnt = new PrintStream(localOut, false);
  153.     boolean localize = localOut != out;
  154.  
  155.     if (header != null) {
  156.         prnt.write('#');
  157.         prnt.println(header);
  158.     }
  159.     prnt.write('#');
  160.     prnt.println(new Date());
  161.  
  162.     for (Enumeration e = keys() ; e.hasMoreElements() ;) {
  163.         String key = (String)e.nextElement();
  164.         prnt.print(key);
  165.         prnt.write('=');
  166.  
  167.         String val = (String)get(key);
  168.         int len = val.length();
  169.         boolean empty = false;
  170.  
  171.         for (int i = 0 ; i < len ; i++) {
  172.         int ch = val.charAt(i);
  173.  
  174.         switch (ch) {
  175.           case '\\': prnt.write('\\'); prnt.write('\\'); break;
  176.           case '\t': prnt.write('\\'); prnt.write('t'); break;
  177.           case '\n': prnt.write('\\'); prnt.write('n'); break;
  178.           case '\r': prnt.write('\\'); prnt.write('r'); break;
  179.  
  180.           default:
  181.             if ((ch < ' ') || (ch >= 127) || (empty && (ch == ' '))) {
  182.             if ((ch > 255) && localize) {
  183.                 prnt.write(ch);
  184.             } else {
  185.                 prnt.write('\\');
  186.                 prnt.write('u');
  187.                 prnt.write((ch >> 12) & 0xF);
  188.                 prnt.write((ch >>  8) & 0xF);
  189.                 prnt.write((ch >>  4) & 0xF);
  190.                 prnt.write((ch >>  0) & 0xF);
  191.             }
  192.             } else {
  193.             prnt.write(ch);
  194.             }
  195.         }
  196.         empty = false;
  197.         }
  198.         prnt.write('\n');
  199.     }
  200.     }
  201.  
  202.     /**
  203.      * Gets a property with the specified key. If the key is not 
  204.      * found in this property list, tries the defaults. This method 
  205.      * returns null if the property is not found.
  206.      * @param key the hashtable key
  207.      */
  208.     public String getProperty(String key) {
  209.     String val = (String)super.get(key);
  210.     return ((val == null) && (defaults != null)) ? defaults.getProperty(key) : val;
  211.     }
  212.  
  213.     /**
  214.      * Gets a property with the specified key and default. If the 
  215.      * key is not found in this property list, tries the defaults. 
  216.      * This method returns defaultValue if the property is not found.
  217.      */
  218.     public String getProperty(String key, String defaultValue) {
  219.     String val = getProperty(key);
  220.     return (val == null) ? defaultValue : val;
  221.     }
  222.  
  223.     /**
  224.      * Enumerates all the keys.
  225.      */
  226.     public Enumeration propertyNames() {
  227.     Hashtable h = new Hashtable();
  228.     enumerate(h);
  229.     return h.keys();
  230.     }
  231.  
  232.     /**
  233.      * List properties, for debugging
  234.      */
  235.     public void list(PrintStream out) {
  236.     out.println("-- listing properties --");
  237.     Hashtable h = new Hashtable();
  238.     enumerate(h);
  239.     for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
  240.         String key = (String)e.nextElement();
  241.         String val = (String)h.get(key);
  242.         if (val.length() > 40) {
  243.         val = val.substring(0, 37) + "...";
  244.         }
  245.         out.println(key + "=" + val);
  246.     }
  247.     
  248.     }
  249.     
  250.     /**
  251.      * Enumerates all key/value pairs in the specified hastable.
  252.      * @param h the hashtable
  253.      */
  254.     private synchronized void enumerate(Hashtable h) {
  255.     if (defaults != null) {
  256.         defaults.enumerate(h);
  257.     }
  258.     for (Enumeration e = keys() ; e.hasMoreElements() ;) {
  259.         String key = (String)e.nextElement();
  260.         h.put(key, get(key));
  261.     }
  262.     }
  263. }
  264.